<style>
	.ui-autocomplete-loading { background: white url('/img/ui-anim_basic_16x16.gif') right center no-repeat; }
</style>
<?php 
echo $this->Html->css('jquery.ui.autocomplete');


echo $this->Html->script('jquery.ui.position');
echo $this->Html->script('jquery.ui.autocomplete');
?>
<script type="text/javascript">
<!--
$(function() {
	$('#chbchon').click(function(){
									if($('#chbchon').prop('checked')==false)
									{
										$(".checkbox").prop('checked',false);
									}
									else
									{
										$(".checkbox").prop('checked',true);
									}
								});
	/*
	$("#textsearch").autocomplete({
		 source: "/phankenhs/autocompletekenh"
		});

	
	$( "#textsearch" ).autocomplete({
			source: function(request, response)
			{
		        $.ajax({
		        	url: "/phankenhs/autocompletekenh",
		               dataType: "json",
		          data: {
		            term : request.term,
		            nhanvien_id:$('#nhanvien_id').val(),
					loaikenh_id:$('#loaikenh_id').val()
		          },
		          success: function(data) {
		            response(data);
		          }
		        });
		      },
		      select: function( event, ui )
		      {
		    	  //updatedatakenh();
			   }
		});
	*/
});

function updatedatakenh()
{
	$.ajax({
	  	async:true,
	  	data:$("#textsearch").closest("form").serialize(),
	  	dataType:"html",
	  	success:function (data, textStatus)
	  	{$("#datakenh").html(data);},
	  	type:"post", url:"\/phankenhs\/dungkenh\/"+$('#$donvi_id').val()
    	 });
}
function validate()
{
	checked=0;
	
	$("input:checkbox").each(function(){
		if ($(this).prop('checked')==true)
		{
			if ($(this).prop('class')=='checkbox')
				checked = checked + 1;
			
		}
		});
	if(checked==0)
	{
		alert('Bạn chưa chọn kênh');
		return false;
	}
	
	return confirm('Bạn có chắc không!');
}
//-->
</script>
<?php if($isAjax==0){?>

<?php echo $form->create('Phankenh',array('onsubmit'=>'return validate()','url'=>array('action'=>'luudungkenh')));?>
<?php echo $form->input('donvi_id',array('id'=>$donvi_id,'type'=>'hidden','value'=>$donvi_id));?>
<div id='idd'>
</div>
<table>
  <tr>
    <td><?php echo $form->input('nhanvien_id',array('id'=>'nhanvien_id','label'=>'Nhân viên','type'=>'select','options'=>$nhanviens,'selected'=>$nhanvien_id));?></td>
    <td><?php echo $form->input('loaikenh_id',array('id'=>'loaikenh_id','label'=>'Loại kênh','type'=>'select','options'=>$loaikenhs,'selected'=>$loaikenh_id));?></td>
  </tr>
  <tr>
  <td>
  <?php echo $form->input('textsearch',array('id'=>'textsearch','label'=>'Tìm kiếm mã kênh','type'=>'text'));?>
  </td>
  <td><?php echo $form->end('  Dừng kênh  ');?></td>
  </tr>
</table>

<div id='datakenh'>
<?php if(!empty($kenhs)){?>
<table id='tables' >
<tr> <th>Chọn<?php echo $form->input('chbchon',array('id'=>'chbchon','label'=>false,'type'=>'checkbox'));?></th>  <th>Mã kênh</th>  <th>Tên kênh</th>  <th>Địa chỉ</th>  <th>Điện thoại</th> </tr>
<?php foreach ($kenhs as $kenh):?>
<tr>
<td> <?php echo $form->checkbox('',array('class'=>'checkbox','name'=>'data[Phankenh][pkid]['.$kenh['Phankenh']['id'].']','value'=>$kenh['Phankenh']['id'],'hidden'=>False, 'checked'=>false));?>
<?php //echo $form->input('',array('hidden'=>False,'class'=>'checkbox','name'=>'data[Phankenh][pkid]['.$kenh['Phankenh']['id'].']','label'=>false,'type'=>'checkbox','value'=>''));?></td>
<td><?= $kenh['Kenh']['makenh']?></t
<td><?= $kenh['Kenh']['tenkenh']?></td>
<td><?= $kenh['Kenh']['diachi']?></td>
<td><?= $kenh['Kenh']['sdt']?></td>
</tr>
<?php endforeach;?>  
</table>
<p>
	<?php
	$this->Paginator->options(array('url' => array($donvi_id,$nhanvien_id,$loaikenh_id)));
	echo $this->Paginator->counter(array(
	'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
	));
	?>	</p>

	<div class="paging">
		<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
	 | 	<?php echo $this->Paginator->numbers();?>
 |
		<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
	</div>
<?php }else {?>
<h2>Không có kênh</h2>
<?php }?>
</div>
<?php $this->Js->get('#nhanvien_id')->event('change',
							$this->Js->request(array('controller' => 'phankenhs', 'action' => 'dungkenh',$donvi_id),
							array('update' => '#datakenh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));?>

<?php $this->Js->get('#loaikenh_id')->event('change',
							$this->Js->request(array('controller' => 'phankenhs', 'action' => 'dungkenh',$donvi_id),
							array('update' => '#datakenh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));?>

<?php 
$this->Js->get('#textsearch')->event('keyup',
							$this->Js->request(array('controller' => 'phankenhs', 'action' => 'dungkenh',$donvi_id),
							array('update' => '#datakenh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));
						
/*$this->Js->get('#textsearch')->event('change',
							$this->Js->request(array('controller' => 'phankenhs', 'action' => 'dungkenh',$donvi_id),
							array('update' => '#datakenh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));*/
?>

<?php }else{?>
<?php if(!empty($kenhs)){?>
<table id='tables' >
<tr> <th>Chọn<?php echo $form->input('chbchon',array('id'=>'chbchon','label'=>false,'type'=>'checkbox'));?></th>  <th>Mã kênh</th>  <th>Tên kênh</th>  <th>Địa chỉ</th>  <th>Điện thoại</th> </tr>
<?php foreach ($kenhs as $kenh):?>
<tr>
<td><?php echo $form->input('',array('hidden'=>False,'class'=>'checkbox','name'=>'data[Phankenh][pkid]['.$kenh['Phankenh']['id'].']','label'=>false,'type'=>'checkbox','value'=>''));?></td>
<td><?= $kenh['Kenh']['makenh']?></td>
<td><?= $kenh['Kenh']['tenkenh']?></td>
<td><?= $kenh['Kenh']['diachi']?></td>
<td><?= $kenh['Kenh']['sdt']?></td>
</tr>
<?php endforeach;?>  
</table>
<p>
	<?php
	$this->Paginator->options(array('url' => array($donvi_id,$nhanvien_id,$loaikenh_id)));
	echo $this->Paginator->counter(array(
	'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
	));
	?>	</p>

	<div class="paging">
		<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
	 | 	<?php echo $this->Paginator->numbers();?>
 |
		<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
	</div>
<?php }else {?>
<h2>Không có kênh</h2>
<?php }?>

<?php }?>